package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     *   根据id查询商品分类
     * @param id  商品分类id
     * @return  商品分类的DTO
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        //根据id查询
        Category category = categoryService.getById(id);
        //返回
        return ResponseEntity.ok(new CategoryDTO(category));
    }


    /**
     * 根据id批量查询
     * @param ids  id的集合
     * @return  分类的集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryList(@RequestParam("ids") List<Long> ids ){
        //查询
        List<Category> categories = categoryService.listByIds(ids);
        //转换1
      /*  List<CategoryDTO> dtoList = new ArrayList<>(categories.size());
        for (Category category : categories) {
            dtoList.add(new CategoryDTO(category));
        }*/
        //转换2
        /*List<CategoryDTO> collect = categories.stream().
                map(category -> new CategoryDTO(category)).collect(Collectors.toList());*/
        //转换3
        List<CategoryDTO> collect = categories.stream().
                map(CategoryDTO::new).collect(Collectors.toList());
        //返回
        return ResponseEntity.ok(collect);
    }

    /**
     * 根据父id查询子分类的集合
     * @param pid  父分类的id
     * @return   子分类的集合
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){
        //select * from tb_category where parent_id = ?
        List<Category> list = categoryService.query().eq("parent_id", pid).list();
        //转换
     /*   List<CategoryDTO> dtoList = CategoryDTO.convertEntityList(list);*/
        List<CategoryDTO> collect = list.stream().
                map(CategoryDTO::new).collect(Collectors.toList());

        //返回
        return ResponseEntity.ok(collect);
    }


        //1.子查询  select * from tb_category where id
        //          in(select category_id from tb_category_brand where brand_id = 18374)

        //2.表关联查询  select c.* from tb_category_brand cb Inner Join
        //              tb_category c On cb.category_id = c.id where cb.brand_id = 18374

        //3.分成两次单表查询    select category_id from tb_category_brand where brand_id =18374;
        //                      select * from tb_category where id in ⬆

    /**
     * 根据品牌id查分类集合
     * @param brandId  品牌id
     * @return  分类集合
     */
    @GetMapping("/of/brand")
    public  ResponseEntity<List<CategoryDTO>> queryCategoryByBrand(@RequestParam("id") Long brandId){
        //查询
        List<CategoryDTO> list = categoryService.queryByBrand(brandId);
        //返回
        return ResponseEntity.ok(list);
    }

}
